<?php
/**
 * Created by PhpStorm.
 * User: Dell
 * Date: 2018-12-26
 * Time: 14:58
 */

namespace app\index\controller;

use app\common\model\GoodsModel;
use think\Controller;
use think\Cookie;

class Goods extends Controller
{
    //查看分类商品
    public function index(){
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where['good_delete'] = 0;

            if (isset($param['good_name'])){
                $where['good_name'] = ['like', '%' . $param['good_name'] . '%'];
            }
            if (isset($param['good_type'])){
                $where['good_type'] = ['like', '%' . $param['good_type'] . '%'];
            }
            if (isset($param['good_classify'])){
                $where['good_classify'] = ['like', '%' . $param['good_classify'] . '%'];
            }

            $goods = new GoodsModel();
            $selectResult = $goods->getGoodsByWhere($where, $offset, $limit);

            $return['total'] = $goods->getAllGoods($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    //查看单个商品
//    public function getOneGoods(){
//        if (request()->isPost()){
//            $param = input('param.');
//            $goods = new GoodsModel();
//            $field = ['good_id','good_name','good_img','good_price','good_origin','good_repertory','good_weight'];
//            $row = $goods->getOneGoods($param['good_id'],$field);
//
//            //点击数+1
//            $goods->where(['bran_id'=>$param['good_id']])->setInc('goods_hits', 1);
//
//            //保存浏览记录
//            if(isset($_COOKIE['his'])){
//                $ids = $_COOKIE['his'];//读取cookie
//                $arr = unserialize($ids);//字符串转回原来的数组
//                $arr[] = $param['good_id'];//当前页面url添加到数组中
//                $arr = array_unique($arr);//除去重复的
//                if(count($arr)>3){//只保存3条访问记录
//                    array_shift($arr);
//                }
//                $ids = serialize($arr);//存储为字符串，
//                setcookie('his',$ids);//保存到cookie当中
//            }else{
//                $url = $param['good_id'];//获取当前页面URL
//                $arr[] = $url;//将当前URL保存到数组中
//                $ids = serialize($arr);//存储为字符串
//                setcookie('his',$ids);//保存到cookie当中
//            }
//
//            return msg(1,'',$row);
//        }
//    }

    //读取历史记录
    public function getHistory(){
        if(Cookie::get('his')){

                $ids = $_COOKIE['his'];//读取cookie
                $arr = unserialize($ids);//字符串转回原来的数组
                $where['good_id'] = ['in', $arr];

            }else{
                $where['good_id'] = 0;
            }

            $where['good_delete'] = 0;
            $goods = new GoodsModel();


            $field = ['good_id','good_name','good_img','good_price'];
            $selectResult = $goods->getgoodsByWhere($where, 0, 3,$field);

            $return['rows'] = $selectResult;
            return json($return);
    }

    //读取热门查询
    public function getHots(){
        $goods = new GoodsModel();
        $where['good_delete'] = 0;

        $field = ['good_id','good_name','good_img','good_price','good_origin','good_repertory','good_weight'];

        $selectResult = $goods->getGoodsByHits($where, 0, 6,$field);

        $return['rows'] = $selectResult;
        return json($return);
    }
}